package com.hxalex.simulationhandle.ui.home.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.common.BasePresenter;
import com.hxalex.simulationhandle.ui.home.adpter.TaskAdpter;
import com.hxalex.simulationhandle.ui.home.model.KeyWordBean;
import com.hxalex.simulationhandle.ui.home.model.TaskBean;
import com.hxalex.simulationhandle.ui.home.model.WebSiteBean;
import com.hxalex.simulationhandle.ui.task.DetailTaskActivity;
import com.hxalex.simulationhandle.util.DatabaseUtil;
import com.hxalex.simulationhandle.widget.TaskPopupWindow;

import org.apache.http.util.EncodingUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hx_Alex on 2018/5/22.
 */

public class TaskPersenter extends BasePresenter implements TaskAdpter.onClickListener,TaskAdpter.onLongClickListener,TaskPopupWindow.onDeleteListener{
    public static final String DETAIL_TASK="detail_task";
    private Context mContext;
    private TaskAdpter adpter;
    public TaskPersenter(Context context)
    {
        this.mContext=context;
    }
    

    public TaskAdpter getTaskListAdater()
    {
        List<TaskBean> taskBeans = DatabaseUtil.selectAllTaskBean();
        adpter =new TaskAdpter(taskBeans,mContext);
        adpter.setOnClickListener(this);
        adpter.setOnLongClickListener(this);
        return adpter;
    }
    
    
    public void freshTaskList(TaskAdpter adpter)
    {
        List<TaskBean> taskBeans = DatabaseUtil.selectAllTaskBean();
        adpter.setmData(taskBeans);
        adpter.notifyDataSetChanged();
    }

    public TaskAdpter getAdpter() {
        return adpter;
    }
    
    public boolean addTask(TaskBean taskBean, List<KeyWordBean> keyWordBeans, List<WebSiteBean> webSiteBeans)
    {
        Long taskId = DatabaseUtil.insertTaskInfo(taskBean);
        boolean isInsertSucc1=false,isInsertSucc2=false;
        if(taskId>0)
        {
            taskBean.setTaskId(taskId);
            isInsertSucc1 = DatabaseUtil.insertkeyWordBeanList(keyWordBeans, taskBean);
            isInsertSucc2 = DatabaseUtil.insertkeyWebSiteBeanList(webSiteBeans, taskBean);
        }
        if(isInsertSucc1 && isInsertSucc2) return true;
        
        return false;
    }
    
    
    public TaskBean getTaskInfo(TaskBean taskBean)
    {
        TaskBean data = DatabaseUtil.selectAllTaskBean(taskBean);
       data.setKeyWordBeanList( DatabaseUtil.selectKeyWordByTaskId(data));
        data.setWebSiteBeanList( DatabaseUtil.selectWebSiteByTaskId(data));
        return data;
    }
    
    
    public void updateTaskInfo(TaskBean taskBean,List<KeyWordBean> keyWordBeans,List<WebSiteBean> webSiteBeans,boolean isUpdateKeyword,boolean isUpdateWebSite)
    {
        DatabaseUtil.updateTaskInfo(taskBean,keyWordBeans,webSiteBeans,isUpdateKeyword,isUpdateWebSite);
    }
    
    
    public List<KeyWordBean> getTaskKeyWords(TaskBean taskBean)
    {
        return  DatabaseUtil.selectKeyWordByTaskId(taskBean);
    }

    public List<WebSiteBean> getWebSiteWords(TaskBean taskBean)
    {
        return  DatabaseUtil.selectWebSiteByTaskId(taskBean);
    }
    
    public List<String> parseKeyWordFile(String path)
    {
        
        List<String> dataList=new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path),"utf-8"));
            String line="";
            
            while ((line=br.readLine())!=null)
            {
                String s = EncodingUtils.getString(line.getBytes(), "utf-8");
                Log.e(TAG,s);
                dataList.add(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return dataList;
    }
    
    public boolean deleteTask(TaskBean taskBean)
    {
       if(DatabaseUtil.deleteTask(taskBean)) return true;
       return false;
    }

    @Override
    public void onClick(View view, int position, TaskBean taskBean) {
        Intent i=new Intent(mContext, DetailTaskActivity.class);
        i.putExtra(DETAIL_TASK,(Serializable) taskBean);
        mContext.startActivity(i);
    }

    @Override
    public void longClick(View view, int position, TaskBean taskBean) {
        TaskPopupWindow popupWindow=new TaskPopupWindow(mContext);
        popupWindow.setTaskBean(taskBean);
        popupWindow.setOnDeleteListener(this);
        if(mContext instanceof Activity)
        {
            Activity activity=(Activity) mContext;
            popupWindow.showAtLocation( activity.findViewById(R.id.layout_main), Gravity.BOTTOM,0,0);
        }
       
    }

    @Override
    public void onDelete() {
        freshTaskList(adpter);
    }
}
